package com.android.djlmapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.login.LoginActivity;
import com.android.djlmapp.activity.my.MySettingActivity;
import com.android.djlmapp.activity.my.WangJiYuanMiMaActivity;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.djlmapp.tools.global.UserInfoUtils;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginPasswordActivity extends BaseActivity {
    @BindView(R.id.ed_ysmm)
    EditText edYsmm;
    @BindView(R.id.ed_xmm)
    EditText edXmm;
    @BindView(R.id.ed_qrmm)
    EditText edQrmm;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.tv_sm)
    TextView tvSm;
    @BindView(R.id.tv_forpwd)
    TextView tvForpwd;
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.tv_top_jump)
    TextView tvTopJump;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;

    public static final String TAG = "djlm-my: ";
    private String type;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_password);
        ButterKnife.bind(this);
        type = getIntent().getStringExtra("type");
        mContext = getActivity();

        initData();
        initView();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        homeIvReadpoint.setVisibility(View.GONE);
        topNotes.setVisibility(View.GONE);

        if ("1".equals(type)) {
            tvName.setText("登录密码");
        } else if ("2".equals(type)) {
            tvName.setText("支付密码");
            edYsmm.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
            edXmm.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
            edQrmm.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);

            edXmm.setHint("密码长度6个字符(仅限数字)");
            edQrmm.setHint("请您再次确认密码(仅限数字)");
            tvSm.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.tv_ok, R.id.tv_forpwd, R.id.ll_top_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_top_back:
                finish();
                break;
            case R.id.tv_ok:
                tvOk.setEnabled(false);
                check();
                break;
            case R.id.tv_forpwd:
                forget();
                break;
        }
    }

    private void forget(){
        if ("1".equals(type)) {
            startActivity(new Intent(this,
                    WangJiYuanMiMaActivity.class).putExtra("PWD_TYPE", type));
        } else if ("2".equals(type)) {
            startActivity(new Intent(LoginPasswordActivity.this,
                    WangJiYuanMiMaActivity.class).putExtra("PWD_TYPE", type));
        }
    }

    private void  check(){
        //关闭键盘
        try {
            View view = getWindow().peekDecorView();
            if (view != null) {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }catch (Exception e){}


        String ymm = edYsmm.getText().toString().trim();
        String xmm = edXmm.getText().toString().trim();
        String qrmm = edQrmm.getText().toString().trim();

        if ("2".equals(type) && (ymm.length() != 6 || xmm.length() != 6 || qrmm.length() != 6)) {
            MyApplication.showToast("密码长度为6位数字");
            tvOk.setEnabled(true);
        }else if (ymm.length() < 6 || xmm.length() < 6 || qrmm.length() < 6) {
            MyApplication.showToast("密码长度最低不能少于6位");
            tvOk.setEnabled(true);
        } else if (! xmm.equals(qrmm)) {
            MyApplication.showToast("确认密码不一致");
            tvOk.setEnabled(true);
        }else {
            //请求接口
            getToNet(ymm, xmm);
        }
    }

    private void getToNet(String ymm, String xmm){
        if(! TextUtils.isEmpty(type)){
            MyApplication.getInstance().getHttpTools().editPwd(ymm, xmm, type, new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    Log.i(TAG, "response: " + response.body());
                    try{
                        JSONObject json = new JSONObject(response.body());
                        ToastUtils.showLongToast(mContext,json.getString("msg"));

                        if (json.optInt("code") == 500){
                            if ("1".equals(type)) {
                                //登录密码修改成功,退出登录
                                UserInfoUtils utils = UserInfoUtils.getUserInfoUtilsInstance();
                                utils.setToken("");
                                utils.setIsLog(false);

                                startActivity(new Intent(mContext, LoginActivity.class)
                                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                                                | Intent.FLAG_ACTIVITY_NEW_TASK));

                            }
                            finish();
                        }

                    }catch(Exception e){
                        ToastUtils.showLongToast(LoginPasswordActivity.this, "数据解析异常"+e.toString());
                    }
                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    ToastUtils.showShortToast(LoginPasswordActivity.this, "请检查您的网络");
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    tvOk.setEnabled(true);
                }
            });
        }
    }


}
